package org.example.elmfront.service.Imp;

import org.example.elmfront.mapper.OrderItemMapper;
import org.example.elmfront.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.elmfront.entity.OrderItem;

import java.time.Instant;
import java.util.List;
@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    // 增加订单项
    @Override
    public void addOrderItem(OrderItem orderItem) {
        Instant now = Instant.now();
        orderItem.setAddedAt(now);  // 设置添加时间
        orderItemMapper.insertOrderItem(orderItem);
    }

    // 删除订单项
    @Override
    public void deleteOrderItem(Integer orderItemId) {
        orderItemMapper.deleteOrderItemById(orderItemId);
    }

    // 更新订单项
    @Override
    public void updateOrderItem(OrderItem orderItem) {
        orderItem.setAddedAt(Instant.now());  // 更新时设置当前时间
        orderItemMapper.updateOrderItem(orderItem);
    }

    // 查询某个订单的所有订单项
    @Override
    public List<OrderItem> getOrderItemsByOrderId(Long orderId) {
        return orderItemMapper.findOrderItemsByOrderId(orderId);
    }

    // 根据 ID 查询订单项
    @Override
    public OrderItem getOrderItemById(Integer id) {
        return orderItemMapper.findOrderItemById(id);
    }
}

